package
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class GameObject  implements ISelectable
	{
		public static const SPEED:int = 25;
		public static const TYPE_1:int = 1;
		public static const TYPE_2:int = 2;
		public static const TYPE_3:int = 3;
		public static const TYPE_4:int = 4;
		
		private var currentLocation:Point;
		public var nextLocation:Point;
		public var type:int;
		public var level:int;
		public var display:Sprite = new Sprite();
		public var inPlay:Boolean = false;
		
		public function GameObject()
		{
			nextLocation = currentLocation = new Point(0,0);
		}
		private var m_isMoving:Boolean = false;
		
		public function isMoving():Boolean{
			return m_isMoving;	
		}
		
		public function forceLocation(gamePoint:Point):void{
			nextLocation = currentLocation = gamePoint;
			updateDisplayPosition();
		}
		
		public static function getRandomType():int{
			var types:Array = [GameObject.TYPE_1, GameObject.TYPE_2, GameObject.TYPE_3, GameObject.TYPE_4];
			return types[Math.floor(Math.random()*types.length)];			
		}
		
		
		public function updateDisplay():void{
			Util.removeChildren(this.display);
			
			var newDisplay:Sprite = Util.getRoundRectSprite(Game.PPS, Game.PPS, Config.getGameObjectConfig(this.type)['color'], 1,20);
			var tf:TextField = new TextField();
			Util.applyStyleH1(tf);
			tf.text = String(this.level);
			tf.autoSize = TextFieldAutoSize.LEFT;
			newDisplay.addChild(tf);
			tf.x = newDisplay.width/2 - tf.width/2;
			tf.y = newDisplay.height/2 - tf.height/2;
			
			this.display.addChild(newDisplay);
		}
		
		public function updateDisplayPosition():void{
			display.x = currentLocation.x * Game.PPS;
			display.y = currentLocation.y * Game.PPS;
		}
		
		public function step(delta:int):void{
			var seconds:Number = delta/1000;
			var deltaPoint:Point = nextLocation.subtract(currentLocation);
			m_isMoving = false;
			
			if(deltaPoint.length > .001){
				currentLocation.x += .2*deltaPoint.x*seconds*SPEED;
				currentLocation.y += .2*deltaPoint.y*seconds*SPEED;
				display.alpha -= .08*seconds*SPEED;
				m_isMoving = true;
			}
			
			updateDisplayPosition();
		}
		
		
		public function hitTestPoint(point:Point):Boolean{
			return display.hitTestPoint(point.x, point.y);
		}
		public function highlite(val:Boolean):void{
			if(val){
				display.filters = [new GlowFilter(0x00ffff)];
			} else {
				display.filters = [];
			}
		}
	}
}